import { defineStore } from 'pinia';
import type { ResponseData, Users } from '@/types';
import { http } from '@/services/http';
import { TOKEN_KEY } from '@/types/constant';
interface UserState {
  totalBook: number;
  token: string;
  userInfo: Users;
}

export const useUserStore = defineStore('user', {
  state: (): UserState => ({
    totalBook: 0,
    token: '',
    userInfo: {
      oldPasswd: '',
      id: 0,
      account_number: '',
      username: '',
      realname: '',
      email: '',
      password: '',
      college: '',
      major: '',
      sex: '',
      phonenumber: '',
      qqnumber: '',
      role: '',
      avatar: '',
      bio: '',
      newPasswd: '',
    },
  }),
  getters: {
    isLoggedIn: (state) => Boolean(state.token),
  },
  actions: {
    updateUserInfo() {
      http.get<ResponseData>('/newusers/all', { id: this.token }).then((res) => {
        this.userInfo = res.data;
      });
    },
    logout() {
      this.token = '';
      localStorage.removeItem('kuaikuai');
    },
  },
});
